package com.xzx.bio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.Scanner;

/**
 *
 * @author xinzhixuan
 * @version 1.0
 * @date 2025/9/16 22:23
 */
public class BIOSocketClient {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket();
        socket.connect(new InetSocketAddress("127.0.0.1", 9999));
        System.out.println("连接成功...");
        new Thread(() -> {
            try {
                handleReply(socket);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }).start();
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextLine()) {
            socket.getOutputStream().write(scanner.nextLine().getBytes());
        }
        socket.getOutputStream().write("hello world".getBytes());

    }

    private static void handleReply(Socket socket) throws IOException {
        byte[] bytes = new byte[1024];
        int len = 0;
        while ((len = socket.getInputStream().read(bytes)) != -1) {
            System.out.println("接收到的数据：" + new String(bytes, 0, len));
        }
    }
}
